package com.milhouz.quizzdroid.db;

import java.util.Random;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import com.milhouz.quizzdroid.bean.Category;
import com.milhouz.quizzdroid.bean.Question;
import com.milhouz.quizzdroid.bean.ResponseList;
import com.milhouz.quizzdroid.bean.User;

public class QuizzDroidDbAPI {

	private static QuizzDroidDbAPI instance;

	private static final Random RANDOM = new Random();

	public static synchronized QuizzDroidDbAPI getInstance() {
		if (instance == null) {
			instance = new QuizzDroidDbAPI();
		}
		return instance;
	}

	public boolean isDataAlreadyLoaded(final Context context) {
		Cursor cursor = context.getContentResolver().query(ContentUris.withAppendedId(QuizzDroidProvider.QUESTIONS_URI, 5), null, null, null, null);
		return cursor.moveToFirst();
	}

	public int countQuestions(final Context context) {
		Cursor cursor = context.getContentResolver().query(QuizzDroidProvider.QUESTIONS_URI, null, null, null, null);
		return cursor.getCount();
	}

	public Question getRandomQuestion(final Context context) {
		int nbQuestions = QuizzDroidDbAPI.getInstance().countQuestions(context);
		return getQuestion(context, RANDOM.nextInt(nbQuestions));
	}

	public Question getQuestion(final Context context, final int questionsId) {
		// Select randomly a question from the database
		final Uri questionUri = ContentUris.withAppendedId(QuizzDroidProvider.QUESTIONS_URI, questionsId);
		Cursor cursor = context.getContentResolver().query(questionUri, null, null, null, null);
		final Question mQuestion = new Question(cursor);

		// Select the question category
		final Uri categoriesUri = ContentUris.withAppendedId(QuizzDroidProvider.CATEGORIES_URI, mQuestion.getCategoryId());
		cursor = context.getContentResolver().query(categoriesUri, null, null, null, null);
		mQuestion.setCategory(new Category(cursor));

		// Select question responses
		final Uri responseUri = Uri.parse("content://" + QuizzDroidProvider.PROVIDER_NAME + "/questions/" + mQuestion.getId() + "/responses");
		cursor = context.getContentResolver().query(responseUri, null, null, null, null);
		mQuestion.setResponses(new ResponseList(cursor));

		return mQuestion;
	}

	public int getCategoryId(final Context context, String categoryName) {
		final Uri categoriesUri = Uri.withAppendedPath(QuizzDroidProvider.CATEGORY_NAME_URI, categoryName);
		Cursor cursor = context.getContentResolver().query(categoriesUri, null, null, null, null);
		if (cursor != null && cursor.moveToFirst()) {
			return cursor.getInt(cursor.getColumnIndex(QuizzDroidConstants.ID));
		} else {
			return -1;
		}
	}

	public User getDefaultUser(final Context context) {
		final Uri userUri = ContentUris.withAppendedId(QuizzDroidUserProvider.USERS_URI, QuizzDroidConstants.DEFAULT_USERID);
		Cursor cursor = context.getContentResolver().query(userUri, null, null, null, null);
		return new User(cursor);
	}

	public void insertDefaultUser(final Context context) {
		User user = getDefaultUser(context);
		if (user == null || user.getUsername() == null) {
			ContentValues values = new ContentValues();
			values.put(QuizzDroidConstants.ID, QuizzDroidConstants.DEFAULT_USERID);
			values.put(QuizzDroidConstants.COLUMN_NAME_USERNAME, QuizzDroidConstants.DEFAULT_USERNAME);
			values.put(QuizzDroidConstants.COLUMN_NAME_TOTAL_QUESTIONS, 0);
			values.put(QuizzDroidConstants.COLUMN_NAME_NB_GOOD_RESPONSES, 0);
			values.put(QuizzDroidConstants.COLUMN_NAME_MEANTIME, 0.0f);
			context.getContentResolver().insert(QuizzDroidUserProvider.USERS_URI, values);
		}
	}

	public void updateUser(final Context context, final User userUpdated) {
		final Uri userUri = ContentUris.withAppendedId(QuizzDroidUserProvider.USERS_URI, QuizzDroidConstants.DEFAULT_USERID);
		ContentValues values = new ContentValues();
		values.put(QuizzDroidConstants.ID, userUpdated.getId());
		values.put(QuizzDroidConstants.COLUMN_NAME_USERNAME, userUpdated.getUsername());
		values.put(QuizzDroidConstants.COLUMN_NAME_TOTAL_QUESTIONS, userUpdated.getTotalQuestions());
		values.put(QuizzDroidConstants.COLUMN_NAME_NB_GOOD_RESPONSES, userUpdated.getNbResponses());
		values.put(QuizzDroidConstants.COLUMN_NAME_MEANTIME, userUpdated.getMeanTimeMs());
		context.getContentResolver().update(userUri, values, null, null);
	}

}
